package com.paul.sys.controller.back;

import com.paul.sys.model.Ptstaff;
import com.paul.sys.model.PublishparkInfo;
import com.paul.sys.service.PublishParkService;
import com.paul.sys.tools.PageData;
import com.paul.sys.util.CommSession;
import com.paul.sys.util.GetSytemTime;
import com.paul.sys.util.ParamUtil;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 羽 on 2015/12/29.
 */
@Controller
@RequestMapping("/back/publish/")
public class PublishparkController {

    private static Logger logger = Logger.getLogger(PublishparkController.class);
    @Autowired
    private PublishParkService publishParkService;

    @RequestMapping("ismenu/publishpark")
    public String publishparkInfo() throws Exception {
        return "back/publishPark/publishPark-list";
    }

    @ResponseBody
    @RequestMapping("list")
    public Map<String, Object> listpublishPark(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> paramAutiority = new HashMap<String, Object>();
        PageData publishs;
        try {
            Ptstaff ptstaff = CommSession.getUser(request);
            ParamUtil util = new ParamUtil(request);
            Map<String, String> param = util.getParamMap();
            paramAutiority.put("unid", ptstaff.getUnid());
            List<PublishparkInfo> Pub = publishParkService.selectAuthority(paramAutiority);
            param.put("dpid", ptstaff.getDepartid());
            param.put("pub", Pub.size() == 0 ? null : "gly");
            publishs = publishParkService.listByPage(param);
            result.put("totalRows", publishs.getPageBean().getTotalNum());
            result.put("curPage", param.get("curPage"));
            result.put("data", publishs.getListData());
            result.put("success", true);
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }

        return result;
    }

    @ResponseBody
    @RequestMapping("delete")
    public String deletePublish(HttpServletRequest request) {
        String ids = request.getParameter("id").toString();
        String[] idArr = ids.split(",");
        String result = "true";
        try {
            publishParkService.deleteByIds(idArr);
        } catch (Exception e) {
            result = "false";
            e.printStackTrace();
            logger.error(e.getMessage());
        }
        return result;
    }


    @ResponseBody
    @RequestMapping("edit")
    public Map<String, Object> setpublish(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();

        try {
            ParamUtil util = new ParamUtil(request);
            Map<String, String> param = util.getParamMap();
            JSONObject jsonObject = JSONObject.fromObject(param);
            PublishparkInfo publish = (PublishparkInfo) JSONObject.toBean(jsonObject, PublishparkInfo.class);
            Ptstaff ptstaff = CommSession.getUser(request);
            publish.setCreatedate(GetSytemTime.getTime());
            publish.setDepartid(ptstaff.getDepartid());
            publish.setname(ptstaff.getUnid());
            if (param.get("flag") != null) {
                publishParkService.insertSelective(publish);
            } else {
                publishParkService.updateByPublishMessage(publish);
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }


    @RequestMapping("edit_page")
    public ModelAndView doModify(HttpServletRequest request) {
        ModelAndView view = null;
        if (request.getParameter("id") != null) {
            view = new ModelAndView("back/publishPark/publishPark-edit");
            PublishparkInfo entity = publishParkService.selectByPrimaryKey(request.getParameter("id"));
            view.addObject("publishInfo", entity);
            view.addObject("type", "edit");
        } else {
            view = new ModelAndView("back/publishPark/publishPark-add");
        }
        return view;
    }


}
